define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cms/area/index' + location.search,
                    // add_url: 'cms/area/add',
                    edit_url: 'cms/area/edit',
                    // del_url: 'cms/area/del',
                    multi_url: 'cms/area/multi',
                    import_url: 'cms/area/import',
                    table: 'cms_area',
                }
            });

            var table = $("#table");

            // 初始化表格
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                pagination: false,
                escape: false,
                fixedRightNumber: 1,
                commonSearch: false,
                search: false,
                columns: [
                    [
                        {checkbox: !0},
                        {
                            field: 'id',
                            title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle"><i class="fa fa-chevron-up"></i></a>',
                            operate: false,
                            width: 60,
                            formatter: Controller.api.formatter.subnode
                        },
                        // {field: 'first', title: __('First')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        // {
                        //     field: 'status',
                        //     width: 80,
                        //     title: __('是否显示'),
                        //     searchList: {"1": __('Yes'), "0": __('No')},
                        //     formatter: Table.api.formatter.toggle
                        // },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document.body).on("click", ".btn-toggle", function (e) {
                $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                var that = this;
                var show = $("i", that).hasClass("fa-chevron-down");
                $("i", that).toggleClass("fa-chevron-down", !show);
                $("i", that).toggleClass("fa-chevron-up", show);
                $("a.btn[data-id][data-pid][data-pid!=0]").closest("tr").toggle(show);
                $(".btn-node-sub[data-pid=0]").data("shown", show);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                title: function (value, row, index) {
                    value = value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                    var caret = row.haschild == 1  ? '<i class="fa fa-caret-right"></i>' : '';
                    value = value.indexOf("&nbsp;") > -1 ? value.replace(/(.*)&nbsp;/, "$1" + caret) : caret + value;

                    return '<a href="javascript:;" data-id="' + row.id + '" data-pid="' + row.pid + '" class="'
                        + (row.haschild == 1 ? 'text-primary' : 'disabled') + ' btn-node-sub">' + value + '</a>';
                },
                subnode: function (value, row, index) {
                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __("点击切换子菜单") + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs ' + (1 == row.haschild  ? "btn-success": "btn-default disabled") + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>'
                }
            },
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
